package io.renren.modules.job.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Random;

@Slf4j
@Component
@RabbitListener(queues = "gll_queue")
public class SendMessage {

    @Autowired
    RedisTemplate redisTemplate;

    @RabbitHandler
    public void sendMessage(String phone) throws IOException {
        String code = "";
        for (int i=0;i<6;i++){
            Random random = new Random();
            code += random.nextInt(10);
        }
        //测试效果不发送短信，只是单纯的输出
//        String uid = "zhengyisky2008";
//        String key = "zaq12wsxcde34rfvbgt5";
//        String url = "http://utf8.api.smschinese.cn/?Uid="+uid+"&Key="+key+"&smsMob="+phone+"&smsText=尊敬的"+phone+":您的验证码为：" + code;
//
//        //返回值为发送了多少条，这里就不打印了
//        CloseableHttpClient httpClient = HttpClients.createDefault();
//        HttpGet httpGet = new HttpGet(url);
//        CloseableHttpResponse response = httpClient.execute(httpGet);

        HashOperations<String,String,String> operations = redisTemplate.opsForHash();
//        operations.put("phone",phone,code);
////        redisTemplate.expire("phone",1, TimeUnit.MINUTES);
        //获取一下redis的验证码后打印
        String pc = operations.get("phone", phone);
        System.out.println(pc);
    }
}
